#include "boardgui.h"

// update board on GUI/or other interface
void BoardGUI::update() {
        // build newStatus from internal bitboards
        QString newStatus = "";
        QString* ranks = new QString[8];
        for (int i=0; i<8; i++) ranks[i] = "";

        // for each rank
        for (int r=7; r>=0; r--) {
            // for all 8 positions in this rank
            for (int i=(r*8); i<((r+1)*8); i++) {
               // check for set pieces and add to rank string
                // white
               if (Bitutils::checkBit(board_.wK, i)) {
                   ranks[r] += "k";
                   continue;
               }
               if (Bitutils::checkBit(board_.wQ, i)) {
                   ranks[r] += "q";
                   continue;
               }
               if (Bitutils::checkBit(board_.wR, i)) {
                   ranks[r] += "r";
                   continue;
               }
               if (Bitutils::checkBit(board_.wB, i)) {
                   ranks[r] += "b";
                   continue;
               }
               if (Bitutils::checkBit(board_.wN, i)) {
                   ranks[r] += "n";
                   continue;
               }
               if (Bitutils::checkBit(board_.wP, i)) {
                   ranks[r] += "p";
                   continue;
               }
               //black
               if (Bitutils::checkBit(board_.bK, i)) {
                   ranks[r] += "l";
                   continue;
               }
               if (Bitutils::checkBit(board_.bQ, i)) {
                   ranks[r] += "w";
                   continue;
               }
               if (Bitutils::checkBit(board_.bR, i)) {
                   ranks[r] += "t";
                   continue;
               }
               if (Bitutils::checkBit(board_.bB, i)) {
                   ranks[r] += "v";
                   continue;
               }
               if (Bitutils::checkBit(board_.bN, i)) {
                   ranks[r] += "m";
                   continue;
               }
               if (Bitutils::checkBit(board_.bP, i)) {
                   ranks[r] += "o";
                   continue;
               }
               // no piece found - give a space " " for output text
               ranks[r] += " ";
            }
            newStatus.append(ranks[r].append("\n"));
        }

        //QString newStatus = "tmvqlvmt\noooooooo\n\n\n\n\npppppppp\nrnbqkbnr";
        setStatus( newStatus );
        qDebug() << "Updated GUI succesfully";

}

// update and show targeted squares
// @param U64 targets
void BoardGUI::showTargetSquares(U64 targets) {
        // build newStatus from internal bitboards
        QString newStatus = "";
        QString* ranks = new QString[8];
        for (int i=0; i<8; i++) ranks[i] = "";

        // for each rank
        for (int r=7; r>=0; r--) {
            // for all 8 positions in this rank
            for (int i=(r*8); i<((r+1)*8); i++) {
               // check for set pieces and add to rank string

                // white
               if (Bitutils::checkBit(board_.wK, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "K";
                   else ranks[r] += "k";
                   continue;
               }
               if (Bitutils::checkBit(board_.wQ, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "Q";
                   else ranks[r] += "q";
                   continue;
               }
               if (Bitutils::checkBit(board_.wR, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "R";
                   else ranks[r] += "r";
                   continue;
               }
               if (Bitutils::checkBit(board_.wB, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "B";
                   else ranks[r] += "b";
                   continue;
               }
               if (Bitutils::checkBit(board_.wN, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "N";
                   else ranks[r] += "n";
                   continue;
               }
               if (Bitutils::checkBit(board_.wP, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "P";
                   else ranks[r] += "p";
                   continue;
               }
               //black
               if (Bitutils::checkBit(board_.bK, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "L";
                   else ranks[r] += "l";
                   continue;
               }
               if (Bitutils::checkBit(board_.bQ, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "W";
                   else ranks[r] += "w";
                   continue;
               }
               if (Bitutils::checkBit(board_.bR, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "T";
                   else ranks[r] += "t";
                   continue;
               }
               if (Bitutils::checkBit(board_.bB, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "V";
                   else ranks[r] += "v";
                   continue;
               }
               if (Bitutils::checkBit(board_.bN, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "M";
                   else ranks[r] += "m";
                   continue;
               }
               if (Bitutils::checkBit(board_.bP, i)) {
                   if (Bitutils::checkBit(targets, i)) ranks[r] += "O";
                   else ranks[r] += "o";
                   continue;
               }

               // target this empty square
               if (Bitutils::checkBit(targets, i)) {
                   ranks[r] += "+";
                   continue;
               }

               // no piece and no target square found - give a space " " for output text
               ranks[r] += " ";
            }
            newStatus.append(ranks[r].append("\n"));
        }

        //QString newStatus = "tmvqlvmt\noooooooo\n\n\n\n\npppppppp\nrnbqkbnr";
        setStatus( newStatus );
       // qDebug() << "Updated GUI marked";

}

// update and mark targeted squares
// @param U64 marks
void BoardGUI::markSquares(U64 marks) {
        // build newStatus from internal bitboards
        QString newStatus = "";
        QString* ranks = new QString[8];
        for (int i=0; i<8; i++) ranks[i] = "";

        // for each rank
        for (int r=7; r>=0; r--) {
            // for all 8 positions in this rank
            for (int i=(r*8); i<((r+1)*8); i++) {
               // check for set pieces and add to rank string
                // marks from parameter
                if (Bitutils::checkBit(marks, i)) {
                    ranks[r] += "x";
                    continue;
                }
                // white
               if (Bitutils::checkBit(board_.wK, i)) {
                   ranks[r] += "k";
                   continue;
               }
               if (Bitutils::checkBit(board_.wQ, i)) {
                   ranks[r] += "q";
                   continue;
               }
               if (Bitutils::checkBit(board_.wR, i)) {
                   ranks[r] += "r";
                   continue;
               }
               if (Bitutils::checkBit(board_.wB, i)) {
                   ranks[r] += "b";
                   continue;
               }
               if (Bitutils::checkBit(board_.wN, i)) {
                   ranks[r] += "n";
                   continue;
               }
               if (Bitutils::checkBit(board_.wP, i)) {
                   ranks[r] += "p";
                   continue;
               }
               //black
               if (Bitutils::checkBit(board_.bK, i)) {
                   ranks[r] += "l";
                   continue;
               }
               if (Bitutils::checkBit(board_.bQ, i)) {
                   ranks[r] += "w";
                   continue;
               }
               if (Bitutils::checkBit(board_.bR, i)) {
                   ranks[r] += "t";
                   continue;
               }
               if (Bitutils::checkBit(board_.bB, i)) {
                   ranks[r] += "v";
                   continue;
               }
               if (Bitutils::checkBit(board_.bN, i)) {
                   ranks[r] += "m";
                   continue;
               }
               if (Bitutils::checkBit(board_.bP, i)) {
                   ranks[r] += "o";
                   continue;
               }
               // no piece found - give a space " " for output text
               ranks[r] += " ";
            }
            newStatus.append(ranks[r].append("\n"));
        }

        //QString newStatus = "tmvqlvmt\noooooooo\n\n\n\n\npppppppp\nrnbqkbnr";
        setStatus( newStatus );
       // qDebug() << "Updated GUI marked";

}
